package com.example.boardroom.newItems;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.boardroom.R;
import com.example.boardroom.demo.Content;
import com.example.boardroom.demo.times.EventTimes;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Objects;

public class SecondNodeProvider extends BaseNodeProvider {
    @Override
    public int getItemViewType() {
        return 1;
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_open_2;
    }

    @Override
    public void convert(@NonNull BaseViewHolder holder, BaseNode baseNode) {
        SecondNode node = (SecondNode) baseNode;
        if (node.status.equals("1")) {
            holder.getView(R.id.col_lay).setBackground(ResourcesCompat.getDrawable(getContext().getResources(), R.drawable.bg_open, null));
        } else {
            holder.getView(R.id.col_lay).setBackground(ResourcesCompat.getDrawable(getContext().getResources(), R.drawable.bg_close, null));
        }
        holder.setText(R.id.tv_status, node.open);

    }

    @Override
    public void onClick(@NonNull BaseViewHolder helper, @NonNull View view, BaseNode data, int position) {
        super.onClick(helper, view, data, position);
        // 查找某位置Node的 父节点
        int parentNode = Objects.requireNonNull(getAdapter()).findParentNode(position);
        LogUtils.i("lufei_node", "当前父节点:" + parentNode + "", "子节点:" + position, "总长度:" + getAdapter().getData().size());

        FirstNode firstNode = (FirstNode) getAdapter().getData().get(parentNode);
        SecondNode node = (SecondNode) data;
        switch (firstNode.dataDTO.deviceType) {
            case "light_control":
                if (node.status.equals("1")) {
                    switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "3", null);
                } else {
                    switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "4", null);
                }

                break;
            case "curtain_control":
                if (node.status.equals("1")) {
                    switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "6", null);
                } else {
                    switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "5", null);
                }
                break;
            case "aircondition_control":
                switch (node.open) {
                    case "1档":
                        switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "48", null);
                        break;
                    case "2档":
                        switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "49", null);
                        break;
                    case "3档":
                        switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "50", null);
                        break;
                    default:
                        switchLuck(String.valueOf(firstNode.dataDTO.id), firstNode.dataDTO.deviceType, "4", null);
                        break;
                }
                break;
            case "access_control":
                openTheAccessControl(firstNode.dataDTO.id);
                break;
            case "touch_terminal":
                switchTVOnAndOff(node.status.equals("1"), firstNode.dataDTO.id);
                break;
        }
    }

    private void switchLuck(String deviceId, String deviceType, String switchStatus, Object param) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("deviceId", deviceId);
        hashMap.put("deviceType", deviceType);
        hashMap.put("switchStatus", switchStatus);
        hashMap.put("param", param);
        String s = GsonUtils.toJson(hashMap);
        OkGo.<String>post(Content.baseUrl + "ipad/device/singleControlAreaDevice")
                .tag(this)
                .upJson(s)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        EventBus.getDefault().post(new EventTimes("", 3));
                    }
                });
    }

    /**
     * 开启门禁
     */
    public void openTheAccessControl(int deviceId) {
        OkGo.<String>post(Content.baseUrl + "deviceMonitor/yjDevice/openDoor")
                .tag(this)
                .params("deviceId", deviceId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {

                        AccessControlBean bean = GsonUtils.fromJson(response.body(), AccessControlBean.class);
                        try {
                            ToastUtils.showShort(bean.data.msg);
                        }catch (Exception e){
                            e.printStackTrace();

                        }
//                        ToastUtils.showShort(bean.data.msg);


                    }
                });

    }

    /**
     * 开关电视
     */
    private void switchTVOnAndOff(boolean IsSwitch, int deviceId) {
        OkGo.<String>post(Content.baseUrl + "link/device/switchScreen?switch=" + IsSwitch)
                .tag(this)
                .params("deviceId", deviceId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        EventBus.getDefault().post(new EventTimes("", 3));
                    }
                });
    }
}
